package com.changgou.goods.controller;

import com.changgou.common.constant.StatusCode;
import com.changgou.common.vo.Result;
import com.changgou.goods.pojo.Album;
import com.changgou.goods.service.AlbumService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/****
 * @Author:www.itheima.com
 * @Description:
 * @Date www.itheima.com
 *****/

@RestController
@RequestMapping("/album")
@CrossOrigin
public class AlbumController {

    @Autowired
    private AlbumService albumService;

    /***
     * Album分页条件搜索实现
     * @param album: 查询条件
     * @param page:当前页
     * @param size:每页显示多少条
     * @return 查询结果
     */
    @PostMapping(value = "/search/{page}/{size}" )
    public Result<PageInfo<Album>> findPage(@RequestBody(required = false)  Album album, @PathVariable  int page, @PathVariable  int size){
        //调用AlbumService实现分页条件查询Album
        PageInfo<Album> pageInfo = albumService.findPage(album, page, size);
        return new Result<PageInfo<Album>>("查询成功！", pageInfo);
    }

    /***
     * Album分页搜索实现
     * @param page:当前页
     * @param size:每页显示多少条
     * @return 查询结果
     */
    @GetMapping(value = "/search/{page}/{size}" )
    public Result<PageInfo<Album>> findPage(@PathVariable  int page, @PathVariable  int size){
        //调用AlbumService实现分页查询Album
        PageInfo<Album> pageInfo = albumService.findPage(page, size);
        return new Result<PageInfo<Album>>("查询成功！", pageInfo);
    }

    /***
     * 多条件搜索品牌数据
     * @param album 查询条件
     * @return 查询结果
     */
    @PostMapping(value = "/search" )
    public Result<List<Album>> findList(@RequestBody(required = false)  Album album){
        //调用AlbumService实现条件查询Album
        List<Album> list = albumService.findList(album);
        return new Result<List<Album>>("查询成功！", list);
    }

    /***
     * 根据ID删除品牌数据
     * @param id 主键
     * @return 查询结果
     */
    @DeleteMapping(value = "/{id}" )
    public Result delete(@PathVariable Long id){
        //调用AlbumService实现根据主键删除
        albumService.delete(id);
        return new Result(true, StatusCode.OK, "删除成功！");
    }

    /***
     * 修改Album数据
     * @param album 更新对象
     * @return 操作结果
     */
    @PutMapping
    public Result update(@RequestBody  Album album){
        //调用AlbumService实现修改Album
        albumService.update(album);
        return new Result(true, StatusCode.OK, "修改成功！");
    }

    /***
     * 新增Album数据
     * @param album 新增对象
     * @return 操作结果
     */
    @PostMapping
    public Result add(@RequestBody  Album album){
        //调用AlbumService实现添加Album
        albumService.add(album);
        return new Result(true, StatusCode.OK, "添加成功！");
    }

    /***
     * 根据ID查询Album数据
     * @param id 主键
     * @return 查询结果
     */
    @GetMapping("/{id}")
    public Result<Album> findById(@PathVariable Long id){
        //调用AlbumService实现根据主键查询Album
        Album album = albumService.findById(id);
        return new Result<Album>("查询成功！", album);
    }

    /***
     * 查询Album全部数据
     * @return 查询结果
     */
    @GetMapping
    public Result<List<Album>> findAll(){
        //调用AlbumService实现查询所有Album
        List<Album> list = albumService.findAll();
        return new Result<List<Album>>("查询成功！", list);
    }
}
